/*
 * Created on 2005-4-20
 * 
 * Copyright bloomsoft.cn All rights reserved
 */
package org.nanhill.xsheet;

import java.io.FileOutputStream;

import junit.framework.TestCase;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.nanhill.commons.html.CssColor;
import org.nanhill.commons.html.CssTag;
import org.nanhill.commons.model.table.Cell;
import org.nanhill.commons.model.table.Row;
import org.nanhill.commons.model.table.Table;
import org.nanhill.xsheet.model.XSheetModel;
import org.nanhill.xsheet.model.XSheetStyle;
import org.nanhill.xsheet.output.excel.DefaultExcelOutputBuilder;
import org.nanhill.xsheet.output.excel.ExcelOutputBuilder;
import org.nanhill.xsheet.output.excel.AbstractExcelOutputDirector;
import org.nanhill.xsheet.output.excel.XSheetFixedCellExcelOutputDirector;
import org.nanhill.xsheet.output.excel.PushCellExcelOutputDirector;

/**
 * @author alin
 */
public class ExcelSheetBuilderSample extends TestCase {
	private void writeExcel(AbstractExcelOutputDirector director,
			XSheetModel model, String relativeFilename) throws Exception {
		ExcelOutputBuilder builder = new DefaultExcelOutputBuilder();
		builder.setModel(model);
		director.setBuilder(builder);
		director.construct();
		HSSFWorkbook wb = builder.getProduct();
		// File outFile = FileKit.getFileFirmly(this.getClass(),
		// relativeFilename);
		FileOutputStream fsout = new FileOutputStream("src/res/xsheet/"
				+ relativeFilename);
		wb.write(fsout);
	}

	public void testFixedCellExcelCreate() throws Exception {
		XSheetModel model = this.getComplexSheetModel();
		this.writeExcel(new XSheetFixedCellExcelOutputDirector(), model,
				"FixedCell.xls");
	}

	public void testPushCellExcelCreate() throws Exception {
		XSheetModel model = this.getComplexSheetModel();
		this.writeExcel(new PushCellExcelOutputDirector(), model,
				"PushCell.xls");
	}

	public void testClashCell() throws Exception {
		XSheetModel model = new XSheetModel();
		Table table;
		Row row;
		Cell cell;
		// --------- table 0 ------------
		table = model.createTable(0, "table 1");
		// --- row 0
		row = table.createRow(0);
		cell = row.createCell("cell-0-0");
		cell.setColspan(2);
		cell = row.createCell("cell-0-1");
		cell.setColspan(2);
		cell = row.createCell("cell-0-2");
		cell.setRowspan(4);
		// --- row 1
		row = table.createRow(1);
		cell = row.createCell("cell-1-0");
		cell = row.createCell("cell-1-1");
		// --- row 3
		row = table.createRow(2);
		cell = row.createCell("cell-2-0");
		cell = row.createCell("cell-2-1");
		cell = row.createCell("cell-2-2");
		cell = row.createCell("cell-2-3");
		cell = row.createCell("cell-2-4");
		cell = row.createCell("cell-2-5");
		// cell.setColspan(2);
		this.writeExcel(new PushCellExcelOutputDirector(), model,
				"PushClashCell.xls");
	}

	public void testInsertRow() throws Exception {
		XSheetModel model = this.getSimpleSheetModel();
		model.getTable(0).insertRow(0).createCell("title");
		model.getTable(0).getRow(1).createCell("cell-0-2");
		this.writeExcel(new PushCellExcelOutputDirector(), model,
				"testInsertRow.xls");
	}

	private XSheetModel getComplexSheetModel() {
		XSheetModel model = new XSheetModel();
		// --------- set style ----------
		model.addStyle(new XSheetStyle().setBgColor(CssColor.YELLOW).setId(1)
				.setAlignment(CssTag.CENTER));
		// --------- table 0 ------------
		Table table = model.createTable(0, "table 1");
		// --- row 0
		Row row = table.createRow(0);
		row.createCell("统计").setSpans(2, 2);
		row.createCell("一月份").setColspan(4);
		// --- row 1
		row = table.createRow(1);
		row.createCell("零售").setStyleId(1);
		row.createCell("批发").setStyleId(1);
		row.createCell("赠送").setStyleId(1);
		row.createCell("促销").setStyleId(1);
		// --- row 2
		row = table.createRow(2);
		row.createCell("杭州").setRowspan(2);
		row.createCell("IP卡");
		row.createCell("729527");
		row.createCell("20");
		row.createCell("5");
		row.createCell("25");
		// --- row 3
		row = table.createRow(3);
		row.createCell("IC卡");
		row.createCell("540110");
		row.createCell("15");
		row.createCell("0");
		row.createCell("15");
		return model;
	}

	private XSheetModel getSimpleSheetModel() {
		XSheetModel model = new XSheetModel();
		Table table;
		Row row;
		Cell cell;
		// --------- table 0 ------------
		table = model.createTable(0, "table 1");
		// --- row 0
		row = table.createRow();
		cell = row.createCell("cell-0-0");
		cell.setColspan(2);
		cell = row.createCell("cell-0-1");
		cell.setColspan(2);
		return model;
	}

	public void testPushOrFixedCell() throws Exception {
		this.writeExcel(new PushCellExcelOutputDirector(), this
				.getSimpleSheetModel(), "testPush.xls");
		this.writeExcel(new XSheetFixedCellExcelOutputDirector(), this
				.getSimpleSheetModel(), "testFixed.xls");
	}

}
